package control;

//import java.util.ArrayList;
import java.util.ArrayList;
import java.util.Date;

import db.DBReservation;
import db.DBReservation_Apartment;
import db.DBReservation_ApartmentType;
import model.Apartment;
import model.ApartmentType;
import model.Renter;
import model.Reservation;
import model.ReservationStatus;
import model.Reservation_Apartment;
import model.Reservation_ApartmentType;

public class CtrReservation {

	private Reservation re;
	private CtrRenter rCtr;
	private DBReservation dbre;
	private DBReservation_ApartmentType dbrat;
	private DBReservation_Apartment dbra;
	private Renter r;
	private CtrApartment aCtr;
	
	public CtrReservation()
	{
		this.rCtr = new CtrRenter();
		this.dbre = new DBReservation();
		this.dbrat = new DBReservation_ApartmentType();
		this.dbra = new DBReservation_Apartment();
		//this.r = new Renter();
		this.aCtr = new CtrApartment();
	}
	
	public void removeReservation(int renterId)
	{
		this.re = null;
		dbre.deleteByRenter(renterId);
	}

	public void createRenter(String firstName, String lastName,String 
			cpr,String mobilePhone, String privatePhone, String address,
			String email, String occupation, String workplace)
	{
		this.r=rCtr.createRenter(firstName, lastName, cpr, mobilePhone, 
				privatePhone, address, email, occupation, workplace);
	}

	public Reservation createReservation(Date moveInDate, int[] apartments, int[] types){
		re = new Reservation();
		if (this.r !=null)
		{
			
			re.setRenter(this.r);
			re.setStatus(ReservationStatus.PENDING);
			re.setMoveInDate(moveInDate);
			re.setReservationDate(new Date());
			re.setId(dbre.insertScope(re));
			//return re;
			for(int aId:apartments)
			{
				this.addApartment(aId);
			}
			for(int tId:types)
			{
				this.addType(tId);
			}	
		}
		return re;
	}
	private void addApartment(int apartmentId){
		Apartment a = this.aCtr.buildApartment(apartmentId);
		this.dbra.insert(new Reservation_Apartment(re,a));
	}
	private void addType(int apTypeId){
		ApartmentType at = this.aCtr.buildApartmentType(apTypeId);
		this.dbrat.insert(new Reservation_ApartmentType(re,at));
	}
	
	public ArrayList<Reservation> getByApartmentType(int apartmentTypeId){
		return dbre.getByApartmentType(apartmentTypeId);
	}

	public ArrayList<Reservation> getByApartment(int apartmentId){
		return dbre.getByApartmentId(apartmentId);
	}
	
	
	
	/*
	public ArrayList<Renter> findRenter(String firstName, String lastName)
	{
		rCtr.findRenter(firstName, lastName);
		return null;
		
	}
	*/
	/*
	public Reservation createReservation(int renterId)
	{
		re = new Reservation();
		re.setRenter(new Renter(renterId));
		re.setStatus(ReservationStatus.PENDING);
		re.setReservationDate(new Date());
		re.setId(dbre.insertScope(re));
		return re;
		
	}
	*/
	
}
